package com.TEAM6.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.TEAM6.beans.BooksBean;

//Book Search - 
public class SearchDAO {
	private Connection  road;
	private PreparedStatement car;
	private ResultSet results;
	private DataSource ds;
	public SearchDAO(){
		// DBCP에 connect 요청
		try{
			Context init = new InitialContext();
			ds = (DataSource)init.lookup("java:comp/env/SJHDB");
		}
		catch(Exception e){
			e.printStackTrace();
			return;
		}
	}
	public ArrayList<BooksBean>searchBooks(String word){
		ArrayList<BooksBean> list = new ArrayList<BooksBean>();
		String sql = "SELECT * FROM BOOKSEARCH WHERE WORD LIKE ?";
		
		
		try {
			road = ds.getConnection();
			car = road.prepareStatement(sql);
			car.setNString(1, "%"+word+"%");
			
			results = car.executeQuery();
			while (results.next()){
				BooksBean e = new BooksBean();
				String[] w =results.getNString("WORD").toString().split("\\|");
				e.setIsbn(w[0]);
				e.setName(w[1]);
				e.setPublicher(w[2]);
				e.setWriter(w[3]);
				e.setImage(results.getNString("BO_IMAGE"));
				
				list.add(e);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return list;
	}
}
